package top.hidoctor.auscultation.utils;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.EntryXComparator;
import com.google.gson.Gson;
import com.tang.util.GsonUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Random;
import java.util.Timer;


public class ChartHelper {

    public static float getValue() {
        DecimalFormat mDecimalFormat = new DecimalFormat("#.00");
        Random mRandom = new Random();
        return Float.valueOf(mDecimalFormat.format(mRandom.nextFloat()));
    }

    public static int maxCount = 7000; //集合最大存储数量

    public static void addEntry2(List<Entry> mData, LineChart lineChart, List<Float> yList) {
        if (lineChart != null && lineChart.getData() != null && lineChart.getData().getDataSetCount() > 0 && yList.size() > 0) {
            //倒序排列
//            maxCount=yList.size();
//            XAxis xAxis = lineChart.getXAxis();
//            xAxis.setAxisMaximum(maxCount);
//            xAxis.setLabelCount(maxCount);

//            Collections.reverse(yList);
            Log.e("TAG", "addEntry2: " + yList.size());
            List<Entry> entryList = new ArrayList<>();
            int x = yList.size();
            for (int i = 0; i < yList.size(); i++) {
                entryList.add(new Entry(x, TextUtils.isEmpty(yList.get(i).toString()) ? 0 : yList.get(i)));
                x--;
            }
            mData.addAll(entryList);
            Log.e("TAG", "addEntry2: " + GsonUtil.toJsonStr(entryList));
            LineData lineData = new LineData(getSet(mData));
            lineData.setDrawValues(false);
            lineChart.setData(lineData);
            lineChart.invalidate();
        }
    }

    public static void addEntry(List<Entry> mData, LineChart lineChart, float yValues) {
        if (lineChart != null && lineChart.getData() != null && lineChart.getData().getDataSetCount() > 0) {
            int size = mData.size();
            if (size == 0) {
                Entry entry = new Entry(maxCount, yValues);
                mData.add(entry);
            } else {
                boolean needRemove = false;
                for (Entry e : mData) {
                    float x = e.getX() - 1;
                    if (x < 0) {
                        needRemove = true;
                    }
                    e.setX(x);
                }
                if (needRemove) {
                    mData.remove(0);
                }
                Entry entry = new Entry(maxCount, yValues);
                mData.add(entry);
            }

            LineData lineData = new LineData(getSet(mData));
            lineData.setDrawValues(false);
            lineChart.setData(lineData);
            lineChart.invalidate();
        }
    }

    public static void initChart(List<Entry> mData, LineChart lineChart, float maxYValue) {
        int lineColor = Color.parseColor("#ebebeb");
        int textColor = Color.parseColor("#999999");

        lineChart.setDragEnabled(false);
        lineChart.setScaleEnabled(false);
        lineChart.getDescription().setEnabled(false);
        lineChart.getLegend().setEnabled(false);
        lineChart.getAxisRight().setEnabled(false);
        lineChart.getXAxis().setEnabled(false);

        YAxis axisLeft = lineChart.getAxisLeft();
        axisLeft.setAxisMinimum(0f);
        axisLeft.setAxisMaximum(maxYValue);
        axisLeft.setLabelCount(10);
        axisLeft.setGridColor(lineColor);
        axisLeft.setDrawLabels(true);
        axisLeft.setDrawGridLines(false);
        axisLeft.setTextColor(textColor);
        axisLeft.setAxisLineColor(lineColor);

        XAxis xAxis = lineChart.getXAxis();
        xAxis.setDrawGridLines(false);
        xAxis.setDrawLabels(false);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setAxisMaximum(maxCount);
        xAxis.setLabelCount(maxCount);
        Collections.sort(mData, new EntryXComparator());

        LineData lineData = new LineData(getSet(mData));
        lineData.setDrawValues(false);
        lineChart.setData(lineData);
        lineChart.invalidate();
    }

    private static LineDataSet getSet(List<Entry> mData) {
        int valueColor = Color.parseColor("#2979FF");
        LineDataSet set = new LineDataSet(mData, "");
        set.setDrawFilled(true);
        set.setFillColor(valueColor);
        set.setColor(valueColor);
        set.setValueTextColor(valueColor);
        set.setDrawCircles(false);
        set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        return set;
    }


}
